package com.yjz.zpay.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.yjz.zpay.R;
import com.yjz.zpay.bean.LoginResult;
import com.yjz.zpay.utils.JSONParser;
import com.yjz.zpay.utils.SpUtils;


/**
 * login
 *
 * @author yjz
 *         created at 2019/1/18 下午9:43
 */

public class LoginActivity extends Activity implements OnClickListener {

    private EditText etAccount, etPwd;
    private Button bt_save;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_login1);

        mContext = this;
        etAccount = (EditText) findViewById(R.id.et_account);
        etPwd = (EditText) findViewById(R.id.et_password);

        bt_save = (Button) findViewById(R.id.save);
        bt_save.setOnClickListener(this);

        String name = SpUtils.getString(mContext, "login_name");
        String pwd = SpUtils.getString(mContext, "login_pwd");

        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
            etAccount.setText(name);
            etPwd.setText(pwd);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save:
                login();
                break;
            default:
                break;
        }
    }

    private void login() {
        final String account = etAccount.getText().toString();
        final String pwd = etPwd.getText().toString();
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(getApplicationContext(), "请输入用户名！", Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(getApplicationContext(), "请输入密码！", Toast.LENGTH_LONG).show();
            return;
        }

        Toast.makeText(getApplicationContext(), "登陆中..请稍后...", Toast.LENGTH_LONG).show();

        OkGo.<String>post("http://yibeipay.com/auth/login")
                .params("username", account)
                .params("password", pwd)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        SpUtils.putString(mContext, "login_name", account);
                        SpUtils.putString(mContext, "login_pwd", pwd);


                        String resp = response.body().toString();
                        LoginResult result = (LoginResult) JSONParser.toObject(resp, LoginResult.class);

                        if (result != null && result.getCode() ==1) {
                            Toast.makeText(getApplicationContext(), result.getMessage(), Toast.LENGTH_LONG).show();

                            Intent intent = new Intent(mContext, MainActivity.class);
                            startActivity(intent);

                            finish();
                        } else {
                            Toast.makeText(getApplicationContext(), result.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }


}
